<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Dexcalibur - Inspectors</title>

    <!-- styles -->
    <!--## pages/inc/tpl_css.html ##-->

    <!--## pages/inc/tpl_js_d3.html ##-->
 
   <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body class="dxc-dark">

    
        <!-- Navigation -->
        <!--## pages/inc/menu.html ##-->

        <div id="page-wrapper" class="dxc-dark">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">Inspectors list (plugins)</h1>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <div class="row">
                <div class="col-lg-9 dxc-description">
                    <p>An <b>inspector</b> is a piece of code listenning events emits from others inspectors, processing data collected by hooks and emitting new events.   
                    Custom inspector can be plugged into the <code>./inspectors/</code> folder (require restart). An inspector can offer custom UI. 
                    The aim of inspectors is to extend the Dexcalibur features by offering a way to correlate data collected by all hooks. An inspector can follows file access, links a function to a file, detects and parse keystore, and so.
                    <a href="https://dexcalibur.org/docs">See help for more information</a> 
                    </p>
                </div>  
            </div>

            
            <!-- /.row -->
            <table width="100%" class="table table-striped table-bordered table-hover dataTable" id="dataTables-inspectors">
                    <thead>
                        <tr>
                            <th></th>
                            <th>Name</th>
                            <th>Description</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
              

        </div>
        <!-- /#page-wrapper -->


    <!-- /#wrapper -->

    <!--## pages/inc/tpl_js_end.html ##-->
    <!--## pages/inc/tpl_ace_js.html ##-->

    <script>

    function setupAceEditor(id){
        var editor = ace.edit(id);
        editor.setTheme("ace/theme/monokai"); //
    
        editor.setOptions({
            maxLines: 50, 
            fontSize: "12pt"
        });
        //editor.setUseWorker(false);
        editor.session.setMode("ace/mode/javascript");
    }
    //    setupAceEditor();
    var IdRegister = new Wexcalibur();

    function editorload(e){
        var editor = ace.edit("editor");
        editor.setTheme("dist/ace/theme/monokai");
        editor.session.setMode("dist/ace/mode/javascript");
        alert("loaded");
    }

    

    function format ( row ) {
        // `d` is the original data object for the row
        let body = '<table class="table table-sm"><tbody>';
        body += '<tr><td>Id</td><td>'+row.id+'</td></tr>';
        body += '<tr><td>Name</td><td>'+row.name+'</td></tr>';
        body += '<tr><td>Description</td><td>'+row.description+'</td></tr></table>';

        let ide = null;
        if(row.prologue != null && row.prologue.script){
            ide = IdRegister.codeReg.next() 
            body += '<h4>Prologue&nbsp;&nbsp;[<a style="font-size:0.8em;cursor:pointer" class="plgdispl" prologue="'+ide+'">hide</a>]</h4><pre id="'+ide+'">'+row.prologue.script+'</pre>';
        }

        body += '<h4>Hooks</h4>';           
        body += '<table class="table table-sm"><thead><tr><th scope="col">Type</th><th scope="col">Name</th></thead><tbody>';
         
        if(row.hooks.probes != null){
            for(let i in row.hooks.probes){
                body += '<tr><td><span class="badge badge-pill badge-primary probe" field="'+btoa(row.hooks.probes[i].method)+'">probe</span>'+
                    '</td><td>'+$('<div />').text(row.hooks.probes[i].method).html()+'</td>'+
                    //'<td><button class="btn btn-primary edithook" style="height:1.5em;padding-top:0px;padding-bottom:0px;">Edit</button>'+
                    //'&nbsp;<button class="btn btn-danger removehook" style="height:1.5em;padding-top:0px;padding-bottom:0px;">Remove</button></td>'+
               '</tr>';
            }
        }

         
        if(row.hooks.intercepts != null){
            for(let i in row.hooks.intercepts){
                body += '<tr>'+
                    '<td><span class="badge badge-pill badge-danger intercept" field="'+btoa(row.hooks.intercepts[i].method)+'">intercepter</span>';

                if(row.hooks.intercepts[i].interceptReplace!=null){
                    body += '&nbsp;<span class="badge badge-pill badge-purple">replace</span>'; 
                }
                if(row.hooks.intercepts[i].interceptBefore!=null){
                    body += '&nbsp;<span class="badge badge-pill badge-info">before</span>'; 
                }
                if(row.hooks.intercepts[i].interceptAfter!=null){
                    body += '&nbsp;<span class="badge badge-pill badge-warning">after</span>'; 
                }

                body +='</td><td>'+$('<div />').text(row.hooks.intercepts[i].method).html()+'</td>'+
             //       '<td><button class="btn btn-primary" style="height:1.5em;padding-top:0px;padding-bottom:0px;">Edit</button>'+
             //       '&nbsp;<button class="btn btn-danger" style="height:1.5em;padding-top:0px;padding-bottom:0px;">Remove</button></td>'+
                '</tr>';
            }
        }
        
        body += '</tbody></table>';

        body += '<h4>Events listeners/emitted</h4>';           
        body += '<table class="table table-sm"><thead><tr><th scope="col">Type</th><th scope="col">Name</th></thead><tbody>';
         
        if(row.events.length != null){
            for(let i in row.events){
                body += '<tr>'+
                    '<td><span class="badge badge-pill badge-info probe" field="'+btoa(row.events[i])+'">emit</span>'+
                    '</td><td>'+$('<div />').text(row.events[i]).html()+'</td>'+
                    //'<td><button class="btn btn-primary edithook" style="height:1.5em;padding-top:0px;padding-bottom:0px;">Edit</button>'+
                    //'&nbsp;<button class="btn btn-danger removehook" style="height:1.5em;padding-top:0px;padding-bottom:0px;">Remove</button></td>'+
               '</tr>';
            }
        }
        if(row.listener.length != null){
            for(let i in row.listener){
                body += '<tr>'+
                    '<td><span class="badge badge-pill badge-primary probe" field="'+btoa(row.listener[i].n)+'">listen</span>'+
                    '</td><td>'+$('<div />').text(row.listener[i].n).html()+'</td>'+
                    //'<td><button class="btn btn-primary edithook" style="height:1.5em;padding-top:0px;padding-bottom:0px;">Edit</button>'+
                    //'&nbsp;<button class="btn btn-danger removehook" style="height:1.5em;padding-top:0px;padding-bottom:0px;">Remove</button></td>'+
               '</tr>';
            }
        }
        body += '</tbody></table>';


        if(ide!=null)
            setTimeout(function(){
                setupAceEditor(ide);
            },20);

        return body;
    }

   

    $(document).ready(function() {

        let clsTable = $('#dataTables-inspectors').DataTable({
            
            paging:   false,
            ordering: false,
            info:     false,
            searching: false,

            ajax: "../api/inspector",
            columns: [
                {
                    "className":      'details-control',
                    "orderable":      false,
                    "data":           null,
                    "defaultContent": ''
                },
                //{   data: 'type'  },
                {   data: 'name'  },
                {   data: 'description' },
                {  
                    render: function(data, type, row, meta){
                        if(row.running){
                            body = '<span class="badge badge-pill badge-success">enable</span>'; 
                        }else{
                            body = '<span class="badge badge-pill badge-secondary">disable</span>'; 
                        }
                        return body;
                    }
                },
                { 
                    render: function(data, type, row, meta ){
                        // <button class="btn btn-info runscan" style="height:1.5em;padding-top:0px;padding-bottom:0px;" scanner="`+btoa(row.id)+`">Run</button>
                        let ret = "";
                        console.log(row, row.id);   
                        if(row.gui_available){
                            ret += `&nbsp;<a href="/inspectors/`+row.id+`/main.html"><span class="fa fa-external-link"></span>&nbsp;Open</a>`;
                        }
                        
                        return ret; //+`
                            //&nbsp;<button style="height:1.5em;padding-top:0px;padding-bottom:0px;" class="btn btn-warning doconfig" inspector="`+btoa(row.id)+`">Configure</button>`;
                    }
                }
            ],
            responsive: true
        });

        $('#dataTables-inspectors tbody').on('click', 'td.details-control', function () {
            var tr = $(this).closest('tr');
            var row = clsTable.row( tr );
            //console.log(row.child);

            if ( row.child.isShown() ) {
                // This row is already open - close it
                row.child.hide();
                tr.removeClass('shown');
            }
            else {
                // Open this row
                row.child( format(row.data()) ).show();
                tr.addClass('shown');
                tr[0].nextElementSibling.childNodes[0].className = 'dxc-expandedrow-bg';
            }
        } );


        $('#dataTables-inspectors tbody').on('click', 'button.showgui', function () {
            let id = $(this).attr("inspector");
            location.href = "/inspectors/"+atob(id)+"/main.html";
        });


        /*
       $('#dataTables-inspectors tbody').on('click', 'a.plgdispl', function () {
            let id = $(this).attr("prologue");
            if( id != null){
                
                let v = $(".plgdispl[prologue="+id+"]").html(); 
                if(v == "show"){
                    $("#"+id).css("display","block");
                    $(".plgdispl[prologue="+id+"]").html("hide");
                }else{
                    $("#"+id).css("display","none");
                    $(".plgdispl[prologue="+id+"]").html("show");
                }
                    
            }else{
                alert('editor not found');
            }
       });*/
       
       /*
        


        $('#dataTables-scanner tbody').on('click', 'button.loadscan', function () {
            let id = $(this).attr("scanner");

            $.ajax("../api/scanner/load",{
                method: "get",
                data: {
                    id: id
                },
                status: {
                    200: function(data,err){
                        location.href="/probe.html";
                    },
                    404: function(data,err){
                        alert("Failed to load bypasser");
                    }
                }
            })
        } );

        $(".runscan").click(function(e){
            console.log(e,this);
//            $("#scannerEditModal").modal();
        });

        $(".loadscan").click(function(){
  //          $("#scannerEditModal").modal();
        });

        $("#newScanner").click(function(){
            $("#scannerEditModal").modal();
        });

        $("#saveScannerBtnModal").click(function(){
            // $("#scannerEditModal").css("display","hidden");
            $("#scannerEditModal").modal();
        });
        */

    });

   

    </script>
</body>

</html>
